/*******************************************************************************
 * Copyright (c) 2008, 2009 Fabio Mauro - Alberto Terraneo.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.2
 * or any later version published by the Free Software Foundation.
 *     
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied 
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 * See the GNU Free Documentation License for more details.
 *     
 * You should have received a copy of the GNU Free Documentation 
 * License along with this distribution; if not, send a email to authors:
 * aterraneo <at> users.sourceforge.net
 * bigmoby09 <at> users.sourceforge.net
 *******************************************************************************/
package it.blueocean.acanto.web.pages.secure.prodotto;

import it.blueocean.acanto.dao.ProdottoDao;
import it.blueocean.acanto.model.Prodotto;
import it.blueocean.acanto.web.platform.components.panel.AcantoPanel;
import it.blueocean.acanto.web.platform.providers.FilteredAcantoProvider;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.Component;
import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DefaultDataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.ISortableDataProvider;
import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.FilterForm;
import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.FilterToolbar;
import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.FilteredAbstractColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.GoAndClearFilter;
import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.IFilterStateLocator;
import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.TextFilteredPropertyColumn;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ListaProdotti extends AcantoPanel
{
	private static final long serialVersionUID = -8379002482509450699L;
	
	private static final Logger log = LoggerFactory.getLogger(ListaProdotti.class);

	@SpringBean
	private ProdottoDao prodottoDao;
	
	private Prodotto selected;
	
	private Label name;
	
	private List<Prodotto> getListaProdotti()
	{
		List<String> eagerAttributes = new ArrayList<String>();
		eagerAttributes.add("listino");
		
		List<Prodotto> listFamiglie = (List<Prodotto>)prodottoDao.findAll(eagerAttributes.toArray(new String[eagerAttributes.size()]));
		
		return listFamiglie;
	}

	public ListaProdotti(String id)
	{
		super(id);
		
		name = new Label("name", "title : lista prodotti");
		name.setOutputMarkupId(true);
		add(name);

		final IFilterStateLocator filteredDataProvider = new FilteredAcantoProvider(getListaProdotti(),"codiceProdotto");

		List<IColumn<Prodotto>> columns = new ArrayList<IColumn<Prodotto>>();

        columns.add(new FilteredAbstractColumn<Prodotto>(new Model<String>("Actions"))
        {
			private static final long serialVersionUID = 1L;

			@Override
			public void populateItem(Item<ICellPopulator<Prodotto>> cellItem, String componentId, IModel<Prodotto> rowModel)
			{
				 cellItem.add(new ActionPanel(componentId, rowModel));				
			}

			@Override
			// return the go-and-clear filter for the filter toolbar
			public Component getFilter(String componentId, FilterForm form) 
			{
				return new GoAndClearFilter(componentId, form);
			}
        });
        columns.add(new TextFilteredPropertyColumn<Prodotto, Object>(new Model<String>("Codice Prodotto"), "codiceProdotto", "codiceProdotto"));
        columns.add(new TextFilteredPropertyColumn<Prodotto, Object>(new Model<String>("Prezzo Unitario"), "prezzoUnitario", "prezzoUnitario"));
        
//        final AcantoDefaultDataTable<Prodotto> documentTable = new AcantoDefaultDataTable<Prodotto>("datatable", columns, filteredDataProvider, 2);
        
        final DefaultDataTable documentTable = new DefaultDataTable("datatable", columns, (ISortableDataProvider) filteredDataProvider, 10);
		final FilterForm form = new FilterForm("filter-form", filteredDataProvider)
		{
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit()
			{
				documentTable.setCurrentPage(0);
			}
		};
        documentTable.addTopToolbar(new FilterToolbar(documentTable, form, filteredDataProvider)); 
        form.add(documentTable); 
        add(form);

        add(new DettaglioProdottoPanel("prodottoPanel"));
	}
	
    class ActionPanel extends Panel
    {
		private static final long serialVersionUID = 1L;

		/**
         * @param id component id
         * @param model model for prodotto
         */
        public ActionPanel(String id, IModel<Prodotto> model)
        {
            super(id, model);
            
            add(new Link<Prodotto>("select")
            {
				private static final long serialVersionUID = 1L;

				@Override
                public void onClick()
                {
                    selected = (Prodotto) getParent().getDefaultModelObject();
                }
            });
        }
    }
    
    class DettaglioProdottoPanel extends Panel
    {
		private static final long serialVersionUID = 1L;
		
		public DettaglioProdottoPanel(String id)
		{
			super(id);
			add(new Label("codiceProdottoLabel", new PropertyModel<Prodotto>(this, "selectedCodiceProdotto")));
		}
		
		public String getSelectedCodiceProdotto()
		{
			if(selected == null)
			{
				return null;
			}
			
			return selected.getCodiceProdotto();
		}
    }

}
